package wxshop.web.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wxshop.api.user.dto.Dto;
import wxshop.api.user.exception.DataNotFoundException;
import wxshop.api.user.model.Balances;
import wxshop.api.user.model.User;
import wxshop.api.user.service.BalancesService;
import wxshop.web.user.dto.FormResult;

import javax.servlet.http.HttpSession;

/**
 * Created by yvdedu.com on 2018/1/26.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@RestController
@RequestMapping("money")
public class MoenyController {
	
	@Autowired
	private BalancesService balancesService;
	
	@Autowired
	private HttpSession session;
	
	
	//拿到余额
	@RequestMapping(method = RequestMethod.GET)
	public Object moeny() {
		return balancesService.getBalance((Integer) session.getAttribute("id"));
	}
	
	//余额记录
	@RequestMapping( value = "/balance", method = RequestMethod.GET)
	public Object moneybalance() {
		return balancesService.queryBalanceDetailed(new Balances((Integer) session.getAttribute("id")));
	}
	
	//提现
	@RequestMapping( value ="/tx", method = RequestMethod.POST)
	public Object txAdd(@RequestBody User user) {
		System.out.println("到这里了");
		System.out.println(user.getBalance());
		System.out.println(session.getAttribute("id"));
		balancesService.withdrawals(new User((Integer) session.getAttribute("id"),-user.getBalance()),new Balances(-user.getBalance(),3, Dto.tint(),(Integer) session.getAttribute("id")));
		return new FormResult(true,"提现成功");
	}
	
	
	//充值
	@RequestMapping( value ="/cz", method = RequestMethod.POST)
	public Object chongzhi(@RequestBody User user) {
		System.out.println("到这里了");
		System.out.println(user.getBalance());
		System.out.println(session.getAttribute("id"));
		balancesService.recharge(new User((Integer) session.getAttribute("id"),+user.getBalance()),new Balances(+user.getBalance(),4, Dto.tint(),(Integer) session.getAttribute("id")));
		return new FormResult(true,"充值成功");
	}
	
	@ExceptionHandler
	public Object on(DataNotFoundException e) {
		return new FormResult(e.getLocalizedMessage());
	}
}
